// pages/kefu-complaint/kefu-complaint.js
import api from '../../utils/api.js'
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        load: true,
        imgs: [],
        msg: "", //留言
        images: [], // 上传到线上的图片地址集合
        shopId: "", // 选中的店铺id
        show: false, // 购买过的商家列表是否展示
        current: 0,
        imgUrl: config.imgUrl,
    },
    // 用户投诉接口
    userAppealShop(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.userAppealShop,
                data: obj,
                success: res => {
                    resolve(res.data)
                    console.log(res.data)
                    if (res.code == 200) {
                        wx.showToast({
                            title: '提交成功',
                            duration: 1500
                        })
                        setTimeout(() => {
                            wx.navigateBack({})
                        }, 1000)
                    } else {
                        wx.showToast({
                            title: '提交失败',
                            duration: 1500,
                            icon: "none"
                        })
                    }
                }
            })
        })
    },
    // 拿到用户购买过的商家
    getHistoryShops() {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.getHistoryShop,
                success: res => {
                    // console.log(res)
                    // if (res.message =="操作完成啊啊啊啊"){
                    //   this.setData({
                    //     historyShops:res.data
                    //   })
                    // }else{
                    //   this.setData({
                    //     historyShops: []
                    //   })
                    // }
                }
            })
        })
    },

    //删除图片
    del(e) {
        var imgs = this.data.imgs
        var images = this.data.images
        imgs.splice(e.target.dataset.id, 1)
        images.splice(e.target.dataset.id, 1)
        this.setData({
            imgs,
            images,
            load: true
        })
    },
    //上传图片
    loadImg() {
        var that = this;
        wx.chooseImage({
            count: 3, //最多可以选择的图片总数  
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: (res) => {
                var tempFilePaths = res.tempFilePaths;
                var imgs = this.data.imgs
                imgs.push(tempFilePaths[0])
                this.setData({
                    imgs,
                })
                if (imgs.length == 4) {
                    this.setData({
                        load: false
                    })
                }
            }
        });
    },
    // 输入留言
    msg(e) {
        this.setData({
            msg: e.detail.value
        })
    },
    //确认发布
    confirm() {
        var that = this
        if (!this.data.msg) {
            wx.showToast({
                title: '请填写留言',
                icon: "none"
            })
            return
        }
        var imgs = this.data.imgs
        var images = []
        let obj = {
            type: 0,
            content: that.data.msg,
            images: [],
            shop_id: "", //(要传)
            shop_address: "",
            client_id: wx.getStorageSync("user_id"),
            order_id: "", //(要传)
        }
        if (imgs.length == 0) {
            that.userAppealShop(obj)
        } else {
            imgs.map((item, index) => {
                wx.uploadFile({
                    url: api.uploadImages,
                    filePath: imgs[index],
                    name: 'images[]',
                    success: function(res) {
                        images.push(res.data)
                        if (imgs.length == index + 1) {
                            obj.images = images
                            that.userAppealShop(obj)
                        }
                    },
                });
            })
        }
    },

    // 显示购买过的商家列表
    open() {
        this.setData({
            show: true
        })
    },
    // 关闭购买过的商家列表
    close() {
        this.setData({
            show: false
        })
    },
    // 选择投诉的商家
    select(e) {
        this.setData({
            current: e.currentTarget.dataset.current,
            show: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // let user_id = wx.getStorageSync('user_id')
        // console.log(user_id)
        this.getHistoryShops()
    },


    onShow: function() {

    }
})